const setStatus = require("../common/setStatus");
const { invoke, query } = require('../tools/index.js');

const MANUFACTURER = ("manufacturer").toUpperCase();
const MANUFACTURERDAILY = ("manufacturerDaily").toUpperCase();
const MANUFACTURERDEAL = ("manufacturerDeal").toUpperCase();
const TREE = ("tree").toUpperCase();

const queryOptions = {
    username: "manufacturer11",
    chaincodeId: "manufacturer",
    fcn: null,
    args: [],
    host: '47.94.106.176',
    org: '1'
}

const invokeOptions = {
    host: "47.94.106.176",
    orderHost:"139.199.191.238",
    org:"1",
    username: "manufacturer11",
    chaincodeId: "manufacturer",
    channelId: "mychannel",
    fcn: null,
    args: [],
}

function setQuery(fcn, args) {
    return Object.assign(queryOptions, {fcn, args});
}

function setInvoke(fcn, args) {
    return Object.assign(invokeOptions, {fcn, args});
}

module.exports = {
    getQuery:async (ctx) => {
        const {QueryId} = ctx.request.query;
        if (!QueryId) {
            setStatus(ctx, 200,0,"","无请求参数id");
            return;
        }
        const data = await query(setQuery("query", [QueryId]));
        setStatus(ctx, 200,1,data);
    },
    // Manufacturer
    getManufacturer: async (ctx) => {
        const {ManufacturerId} = ctx.request.query;
        if (!ManufacturerId) {
            console.log("无请求参数id")
            setStatus(ctx, 200,0,"","无请求参数id");
            return;
        }
        const data = await query(setQuery("query", [ManufacturerId]));
        setStatus(ctx, 200,1,data);
    },
    getManufacturerAll:async (ctx) => {
        const data = await query(setQuery("queryAll", ["manufacturer"]))
        setStatus(ctx, 200,1,data);
    },
    createManufacturer:  async (ctx) => {
        const {Name,ManufacturerOwner,Address,Soil,Temperature} = ctx.request.body
        console.log(Name,ManufacturerOwner,Address,Soil,Temperature)
        if (!Name||!ManufacturerOwner||!Address||!Soil||!Temperature) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        const ManufacturerId = MANUFACTURER + new Date().getTime();
        const args = [ManufacturerId,Name,ManufacturerOwner,Address,Soil,Temperature];
        await invoke(setInvoke("create", ["manufacturer", ...args]))
        setStatus(ctx, 200,1,"");
    },
    changeManufacturer: async (ctx) => {
        const {ManufacturerId,Name,ManufacturerOwner,Address,Soil,Temperature} = ctx.request.body;
        if(!ManufacturerId||!Name||!ManufacturerOwner||!Address||!Soil||!Temperature) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        const args = [ManufacturerId,Name,ManufacturerOwner,Address,Soil,Temperature];
        await invoke(setInvoke("change", [...args]));
        setStatus(ctx, 200,1, "");
    },
    delManufacturer: async (ctx) => {
        const {ManufacturerId} = ctx.request.body;
        if(!ManufacturerId) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        await invoke(setInvoke("delete", [ManufacturerId]));
        setStatus(ctx, 200,1,"");
    },

    // ManufacturerDeal
    getManufacturerDeal: async (ctx) => {
        const {ManufacturerDealId} = ctx.request.query;
        if (!ManufacturerDealId) {
            setStatus(ctx, 200,0,"","无请求参数id");
            return;
        }
        const data = await query(setQuery("query", [ManufacturerDealId]))
        setStatus(ctx, 200,1,data);
    },
    getManufacturerDealAll:async (ctx) => {
        const data = await query(setQuery("queryAll", ["manufacturerDeal"]))
        setStatus(ctx, 200,1,data);
    },
    createManufacturerDeal:  async (ctx) => {
        const {TreesId,PickingTreatment,TotalAmount,DealersId,Price,ManufacturerDealUnit} = ctx.request.body;
        if (!TreesId||!PickingTreatment||!TotalAmount||!DealersId||!Price||!ManufacturerDealUnit) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        const ManufacturerDealId = MANUFACTURERDEAL + new Date().getTime();
        const args = [ManufacturerDealId,TreesId,PickingTreatment,TotalAmount,DealersId,Price,ManufacturerDealUnit];
        await invoke(setInvoke("create", ["manufacturerDeal", ...args]))
        setStatus(ctx, 200,1, "");
    },
    changeManufacturerDeal: async (ctx) => {
        const {ManufacturerDealId,TreesId,PickingTreatment,TotalAmount,DealersId,Price,ManufacturerDealUnit} = ctx.request.body;
        if (!ManufacturerDealId||!TreesId||!PickingTreatment||!TotalAmount||!DealersId||!Price||!ManufacturerDealUnit) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        const args = [ManufacturerDealId,TreesId,PickingTreatment,TotalAmount,DealersId,Price,ManufacturerDealUnit];
        await invoke(setInvoke("change", [...args]))
        setStatus(ctx, 200,1, "");
    },
    delManufacturerDeal: async (ctx) => {
        const {ManufacturerDealId} = ctx.request.body;
        if(!ManufacturerDealId) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        await invoke(setInvoke("delete", [ManufacturerDealId]))
        setStatus(ctx, 200,1, "");
    },

    // ManufacturerDaily
    getManufacturerDaily: async (ctx) => {
        const {ManufacturerDailyId} = ctx.request.query;
        if (!ManufacturerDailyId) {
            setStatus(ctx, 200,0,"","无请求参数id");
            return;
        }
        const data = await query(setQuery("query", [ManufacturerDailyId]));
        setStatus(ctx, 200,1,data);
    },
    getManufacturerDailyAll:async (ctx) => {
        const data = await query(setQuery("queryAll", ["manufacturerDaily"]));
        setStatus(ctx, 200,1,data);
    },
    createManufacturerDaily:  async (ctx) => {
        const {TreeId,Fertilizer,Pesticide,Watering,FruitSize,FruitSizeUnit} = ctx.request.body;
        if (!TreeId||!Fertilizer||!Pesticide||!Watering||!FruitSize||!FruitSizeUnit) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        const ManufacturerDailyId = MANUFACTURERDAILY + new Date().getTime();
        const args = [ManufacturerDailyId,TreeId,Fertilizer,Pesticide,Watering,FruitSize,FruitSizeUnit];
        await invoke(setInvoke("create", ["manufacturerDaily", ...args]))
        setStatus(ctx, 200,1, "");
    },
    changeManufacturerDaily: async (ctx) => {
        const {ManufacturerDailyId,TreeId,Fertilizer,Pesticide,Watering,FruitSize,FruitSizeUnit} = ctx.request.body;
        if (!ManufacturerDailyId||!TreeId||!Fertilizer||!Pesticide||!Watering||!FruitSize||!FruitSizeUnit) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        const args = [ManufacturerDailyId,TreeId,Fertilizer,Pesticide,Watering,FruitSize,FruitSizeUnit];
        await invoke(setInvoke("change", [ ...args]))
        setStatus(ctx, 200,1, "");
    },
    delManufacturerDaily: async (ctx) => {
        const {ManufacturerDailyId} = ctx.request.body;
        if(!ManufacturerDailyId) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        await invoke(setInvoke("delete", [ManufacturerDailyId]))
        setStatus(ctx, 200,1, "");
    },

    // Tree
    getTree: async (ctx) => {
        const {TreeId} = ctx.request.query;
        if (!TreeId) {
            setStatus(ctx, 200,0,"","无请求参数id");
            return;
        }
        const data = await query(setQuery("query", [TreeId]));
        setStatus(ctx, 200,1,data);
    },
    getTreeAll:async (ctx) => {
        const data = await query(setQuery("queryAll", ["tree"]));
        setStatus(ctx, 200,1,data);
    },
    createTree:  async (ctx) => {
        console.log(ctx.request.body)
        const {Name,Area} = ctx.request.body;
        if (!Name||!Area) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        const TreeId = TREE + new Date().getTime();
        const args = [TreeId,Name,Area];
        await invoke(setInvoke("create", ["tree",...args]))
        setStatus(ctx, 200,1, "");
    },
    changeTree: async (ctx) => {
        const {TreeId,Name,Area} = ctx.request.body;
        if(!TreeId||!Name||!Area) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        const args = [TreeId,Name,Area];
        await invoke(setInvoke("change", [...args]))
        setStatus(ctx, 200,1, "");
    },
    delTree: async (ctx) => {
        const {TreeId} = ctx.request.body;
        if(!TreeId) {
            setStatus(ctx, 200,0,"","缺少参数");
            return;
        }
        await invoke(setInvoke("delete", [TreeId]))
        setStatus(ctx, 200,1, "");
    },
};